package com.open.shitou.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;

import com.open.shitou.R;
import com.open.shitou.adapter.TabPageAdapter;
import com.open.shitou.fragment.HomeFragment;
import com.open.shitou.fragment.MineFragment;
import com.open.shitou.fragment.PubGoodsFragment;
import com.open.shitou.uilts.GlideImageLoader;
import com.open.shitou.view.OpenViewPager;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends BaseActivity {

    private static final int HOME_CODE = 0;
    private static final int PUB_GOODS_CODE = 1;
    private static final int MY_MINE_CODE = 2;

    @BindView(R.id.tab_home_rb)
    RadioButton tabHomeRb;
    @BindView(R.id.tab_pub_btn)
    Button tabPubBtn;
    @BindView(R.id.tab_mine_rb)
    RadioButton tabMineRb;
    @BindView(R.id.home_viewpager)
    OpenViewPager homeViewpager;

    public Context mContext;
    public ArrayList<Fragment> mFragmentList = new ArrayList<>();
    public TabPageAdapter mFragmentPageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();
        ButterKnife.bind(this);
        initAllViews();
        //
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 250);
        } else {
            TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//            Contants.IMEI = TelephonyMgr.getDeviceId();
        }
    }

    private void initAllViews() {
        // 下部工具栏
        tabHomeRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    tabMineRb.setChecked(false);
                }
            }
        });
        tabMineRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    tabHomeRb.setChecked(false);
                }
            }
        });
        // 初始化viewpager.
        HomeFragment homeFragment = HomeFragment.newInstance("");
        MineFragment mineFragment = MineFragment.newInstance("");
        PubGoodsFragment pubGoodsFragment = new PubGoodsFragment();
        mFragmentList.add(homeFragment);
        mFragmentList.add(pubGoodsFragment);
        mFragmentList.add(mineFragment);
        mFragmentPageAdapter = new TabPageAdapter(getSupportFragmentManager(),
                mContext, mFragmentList);
        homeViewpager.setAdapter(mFragmentPageAdapter);
        homeViewpager.setCurrentItem(0, false); // 默认页面.
        homeViewpager.setOffscreenPageLimit(3); // 缓存页数.
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    }

    @OnClick({R.id.tab_home_rb, R.id.tab_pub_btn, R.id.tab_mine_rb})
    public void onTabItemCliked(View view) {
        switch (view.getId()) {
            case R.id.tab_home_rb:
                homeViewpager.setCurrentItem(HOME_CODE);
                break;
            case R.id.tab_pub_btn: // 进入发布界面
//                Intent intents = new Intent(mContext, PubGoodsActivity.class);
//                startActivity(intents);
                homeViewpager.setCurrentItem(PUB_GOODS_CODE);
                tabMineRb.setChecked(false);
                tabHomeRb.setChecked(false);
                break;
            case R.id.tab_mine_rb:
                homeViewpager.setCurrentItem(MY_MINE_CODE);
                break;
        }
    }

}
